<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\CompanyLogic;
use Illuminate\Http\Request;

/**
 * 公司管理
 */
class CompanyController extends Controller
{

    protected $logic;
    public function __construct()
    {
        $this->logic = new CompanyLogic();
    }

    public function getCompanyList(Request $request)
    {
        $rules = [
            'short_name'=>'present|nullable|string',
            'name'=>'present|nullable|string',
            'status'=>'present|nullable|numeric|in:0,1,2',
            'page'=>'required|numeric|min:1',
            'page_size'=>'required|numeric|min:10'
        ];

        $params = $request->all();

        $this->verify($params,$rules);
        try{
           $list = $this->logic->getList($params);
           ret_json('ok','请求成功',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //编辑公司
    public function editCompany(Request $request)
    {
        $rules = [
            'id'=>'required|exists:company,id',
            'short_name'=>'required|string',
            'name'=>'required|string',
            'status'=>'required|numeric|in:0,1',
            'tax'=>'required|numeric',
            'cwid'=>'required|numeric',
            'cgid'=>'required|numeric',
            'cnid'=>'required|numeric',
            'xzid'=>'required|numeric',
            'sjid'=>'required|numeric',
            'cgjlid'=>'required|numeric',
            'fzid'=>'required|numeric',
            'zjlid'=>'required|numeric',
            'deptid'=>'required|array',
        ];
        $this->verify($request->all(),$rules);
        try{
            $result = $this->logic->editCompany($request->all());
            if($result) ret_json('ok','编辑成功');
            else ret_json('err','编辑失败');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //添加公司
    public function addCompany(Request $request)
    {
        try{
            $rules = [
                'short_name'=>'required|string',
                'name'=>'required|string',
                'tax'=>'required|numeric',
                'cwid'=>'required|numeric',
                'cgid'=>'required|numeric',
                'cnid'=>'required|numeric',
                'xzid'=>'required|numeric',
                'sjid'=>'required|numeric',
                'cgjlid'=>'required|numeric',
                'fzid'=>'required|numeric',
                'zjlid'=>'required|numeric',
                'deptid'=>'required|array',
            ];
            $this->verify($request->all(),$rules);
            $result = $this->logic->addCompany($request->all());
            if($result) ret_json('ok','添加成功',$result);
            else ret_json('err','添加失败');

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
}
